/**************************************************************
 *@ProjectName: blweibo
 *@FileName: ImageViewerDialog.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.zhiweibo.util.HttpUtil;

/**
 * The Class ImageViewerDialog.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class ImageViewerDialog extends AlertDialog {

    /** The inflater. */
    private final LayoutInflater inflater;

    /** The iv. */
    private final ImageView iv;

    /** The layout. */
    private final LinearLayout layout;

    /** The pb. */
    private final ProgressBar pb;

    /** The picfile path. */
    String picfilePath;

    /** The uri. */
    private final String uri;

    /** The context. */
    private Context context;

    /**
     * Instantiates a new image viewer dialog.
     * 
     * @param paramContext
     *            the param context
     * @param paramString
     *            the param string
     * @param paramDrawable
     *            the param drawable
     */
    public ImageViewerDialog(Context paramContext, String paramString, Drawable paramDrawable) {
        super(paramContext);
        context = paramContext;
        inflater = LayoutInflater.from(paramContext);
        layout = (LinearLayout) inflater.inflate(R.layout.imageviewerdialog, null);
        iv = (ImageView) layout.findViewById(R.id.ivImage);
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View paramView) {
                dismiss();
            }
        });
        pb = (ProgressBar) layout.findViewById(R.id.ProgressBar01);
        pb.setBackgroundDrawable(paramDrawable);
        uri = paramString;
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.AlertDialog#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(layout);
        new LoadPictureTask().execute(0);
    }

    /**
     * Inits the image.
     * 
     * @param bm
     *            the bm
     */
    private void initImage(Bitmap bm) {
        if (bm == null) {
            context.sendBroadcast(new Intent("com.hbr.weibo.noimage"));
            dismiss();
        } else {
            iv.setImageBitmap(bm);
        }
    }

    /**
     * The Class LoadPictureTask.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class LoadPictureTask extends AsyncTask<Object, Integer, Bitmap> {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Bitmap doInBackground(Object... paramArrayOfObject) {
            Bitmap bm = HttpUtil.getImage(getContext(), uri);
            if (bm != null) {
                int width = bm.getWidth();
                int height = bm.getHeight();
                DisplayMetrics metrics = new DisplayMetrics();
                getWindow().getWindowManager().getDefaultDisplay().getMetrics(metrics);
                int newWidth = metrics.widthPixels;
                int newHeight = metrics.heightPixels;
                if (width > newWidth || height > newHeight) {
                    float scaleWidth = ((float) newWidth) / width;
                    float scaleHeight = ((float) newHeight) / height;
                    float scale = Math.min(scaleWidth, scaleHeight);
                    Matrix matrix = new Matrix();
                    matrix.postScale(scale, scale);
                    bm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
                }
            }
            return bm;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(Bitmap paramArrayOfObject) {
            pb.setVisibility(View.GONE);
            initImage(paramArrayOfObject);
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPreExecute()
         */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }
    }
}